package com.shuang.controller;

import com.shuang.pojo.Clazz;
import com.shuang.pojo.ClazzQueryParam;
import com.shuang.pojo.PageResult;
import com.shuang.pojo.Result;
import com.shuang.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /*班级列表分页查询*/
    @GetMapping
    public Result Page(ClazzQueryParam clazzQueryParam){
        log.info("班级查询请求参数：{}", clazzQueryParam);
        PageResult<Clazz> clazzPageResult = clazzService.page(clazzQueryParam);
        return Result.success(clazzPageResult);
    }

    /*新增班级*/
    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        log.info("新增班级信息：{}", clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    /*根据班级id查询回显*/
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id = {}查询班级信息", id);
        Clazz clazz = clazzService.getInfo(id);
        return Result.success(clazz);
    }

    /*更新班级信息*/
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("更新班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /*删除班级*/
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id = {}删除班级", id);
        clazzService.delete(id);
        return Result.success();
    }

    /*查询所有班级*/
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有班级信息");
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

}
